package com.storage.mongo;
import java.net.UnknownHostException;

import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoException;

public class Test1 {

//	Mongo m = new Mongo();
//	// or
//	Mongo m = new Mongo( "localhost" );
//	// or
//	private Mongo m; 

	
	/**
	 * @param args
	 * @throws MongoException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, MongoException {
		
		Mongo m = new Mongo( "192.168.29.135" , 27017 );
		//Mongo m = new Mongo( "localhost" , 27017 );
		DB db = m.getDB("mydb");
		
		//retrieves a collection or creates one if one does not exist
		DBCollection coll = db.getCollection("testCollection");
		
		/*//This section creates a document in JSON format
		BasicDBObject doc = new BasicDBObject();
		
		doc.put("name", "MongoDB");
        doc.put("type", "database");
        doc.put("count", 1);

        //creates a nested document and inserts it into the original document
        BasicDBObject info = new BasicDBObject();

        info.put("x", 203);
        info.put("y", 102);
        info.put("z", 2323);

        doc.put("info", info);

        //inserts the document
        coll.insert(doc);*/
        
        //retrieves the document we just inserted and prints it out
        DBObject myDoc = coll.findOne();
        System.out.println(myDoc);
        
        System.out.println("num Docs-> " + coll.getCount());
        
        //Using a Cursor to Get All the Documents
       /* DBCursor cur = coll.find();

        while(cur.hasNext()) {
            System.out.println(cur.next());
        }*/
        
        //Getting A Single Document with A Query
        DBCursor cur2 = coll.find();
        BasicDBObject query = new BasicDBObject();

        query.put("count", 1); // get doc with count=1

        cur2 = coll.find(query);

        while(cur2.hasNext()) {
            System.out.println(cur2.next());
        }

	}

}
